﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Animation;
using MapLibrary.Players.CollisionResponse;
using MapLibrary.Players;
using Enemies;
using MapLibrary.Players.Skills;

namespace MapLibrary.Particles
{
    /// <summary>
    /// At its simplest, a Particle is simply a Sprite with a lifetime associated with it.  The
    /// particle will exist in the level as long as its lifetime allows it.  After its lifetime
    /// is up, the Particle will remove itself from the level, and reclaim itself into the pool.
    /// 
    /// A Particle has a reference to the ParticleManager that created it.
    /// </summary>
    public class Particle : Sprite
    {
        private int lifeTime;
        private int lifeTimeCounter;

        public Particle() : base()
        {
            this.lifeTime = 0;
            this.CollisionCategory = CollisionResponseManager.CollisionCategory.Particle;

            Hookable = false;
        }

        /// <summary>
        /// You must call Init before working with the particle.
        /// </summary>
        /// <param name="lifeTime"></param>
        public void Init(int lifeTime)
        {
            this.lifeTime = lifeTime;
            this.lifeTimeCounter = 0;
        }

        /// <summary>
        /// Do normal Sprite update routine, but also see if the particle
        /// has surpassed its lifetime.
        /// </summary>
        public override void Update()
        {
            base.Update();

            if(lifeTime != 0)
                UpdateTimer();
        }

        /// <summary>
        /// How many frames the Particle will live in the level.
        /// </summary>
        public int LifeTime
        {
            get { return lifeTime; }
            set { lifeTime = value; }
        }

        public string ParticleType
        {
            get;
            set;
        }

        public override bool MyOnCollision(FarseerPhysics.Dynamics.Fixture f1, FarseerPhysics.Dynamics.Fixture f2, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            bool ret = base.MyOnCollision(f1, f2, contact);

            Player p = f2.Body.UserData as Player;

            if (p != null)
                return false;

            Enemy t = f2.Body.UserData as Enemy;
            if (t != null)
                return false;

            Bullet b = f2.Body.UserData as Bullet;
            if (b != null)
                return false;

            NetSprite ns = f2.Body.UserData as NetSprite;
            if (ns != null)
            {
                return false;
            }

            return ret;
        }

        private void UpdateTimer()
        {
            lifeTimeCounter++;
            if (lifeTimeCounter >= lifeTime)
            {
                lifeTimeCounter = 0;
                RemoveSelf();
            }
        }

        protected virtual void RemoveSelf()
        {
            Type.States[Type.CurrentState].Reset();
            World.ParticleManager.Pools[ParticleType].Reclaim(this);
        }
    }
}
